#include "MarkerRenderer.h"

namespace TUMAR { namespace Render {
	MarkerRenderer::MarkerRenderer(void)
	{
	}

	MarkerRenderer::~MarkerRenderer(void)
	{
	}

	void MarkerRenderer::OnMarkersFound(const MarkerContainer& markers)
	{
		this->markers.assign(markers.begin(), markers.end());
		
		Invalidate();
	}

	void MarkerRenderer::Render(void)
	{
		cv::Size imageSize(640, 480);

		glMatrixMode(GL_PROJECTION);
		glPushMatrix();

		glLoadIdentity();
		gluOrtho2D(imageSize.width / -2.0, imageSize.width / 2.0, imageSize.height / -2.0, imageSize.height / 2.0);
		
		{
			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();
			
			for(MarkerContainer::iterator it = markers.begin(); it != markers.end(); ++it)
			{
				glBegin(GL_LINE_LOOP);

				glVertex2f((*it).SubPixelCorners.at(0).x, (*it).SubPixelCorners.at(0).y);
				glVertex2f((*it).SubPixelCorners.at(1).x, (*it).SubPixelCorners.at(1).y);
				glVertex2f((*it).SubPixelCorners.at(2).x, (*it).SubPixelCorners.at(2).y);
				glVertex2f((*it).SubPixelCorners.at(3).x, (*it).SubPixelCorners.at(3).y);

				glEnd();
			}
			
			glPopMatrix();
		}

		glPopMatrix();
	}
}}